package common;

import java.text.SimpleDateFormat;
import java.util.Date;

import dicts.GlobalDict;
import entities.TCard;
import entities.TMerchant;
import utils.DigestUtil;
import utils.StringUtil;

public class Card {

	public final static String MASTER = "master";
	public final static String SLAVE = "slave";
	
	public static String code(String type) {
		if (type.equals("VISA")) {
			return "001";
		}
		if (type.equals("MASTER")) {
			return "002";
		}
		if (type.equals("JCB")) {
			return "007";
		}
		if (type.equals("AE")) {
			return "003";
		}
		if (type.equals("CLUB")) {
			return "005";
		}
		if (type.equals("DISCOVER")) {
			return "004";
		}
		return "001";
	}

	public static String turn(String type) {
		if (type.equals("VISA")) {
			return "V";
		}
		if (type.equals("MASTER")) {
			return "M";
		}
		if (type.equals("JCB")) {
			return "J";
		}
		if (type.equals("AE")) {
			return "A";
		}
		if (type.equals("CLUB")) {
			return "C";
		}
		if (type.equals("DISCOVER")) {
			return "D";
		}
		return null;
	}

	public static String type(String card) {
		if (isMaster(card)) {
			return "MASTER";
		}
		if (isVisa(card)) {
			return "VISA";
		}
		if (isAe(card)) {
			return "AE";
		}
		if (isJcb(card)) {
			return "JCB";
		}
		if (isClub(card)) {
			return "CLUB";
		}
		if (isDiscover(card)) {
			return "DISCOVER";
		}
		return null;
	}

	public static boolean isVisa(String card) {
		if (card.startsWith("4")) {
			return true;
		}
		return false;
	}

	public static boolean isMaster(String card) {
		if (card.startsWith("5") || card.startsWith("2")) {
			return true;
		}
		return false;
	}

	public static boolean isJcb(String card) {
		if (card.startsWith("35")) {
			return true;
		}
		return false;
	}

	public static boolean isAe(String card) {
		if (card.startsWith("34") || card.startsWith("37")) {
			return true;
		}
		return false;
	}

	public static boolean isClub(String card) {
		if (card.startsWith("30") || card.startsWith("36") || card.startsWith("38") || card.startsWith("39")) {
			return true;
		}
		return false;
	}

	public static boolean isDiscover(String card) {
		if (card.startsWith("60")) {
			return true;
		}
		return false;
	}

	public static boolean isEffective(String crdNumber, TMerchant merchant, String bankType) {
		if (bankType.equals(Card.MASTER)) {
			if (Card.isVisa(crdNumber) && !merchant.getMerVe().equals("1")) {
				return false;
			}
			if (Card.isMaster(crdNumber) && !merchant.getMerMe().equals("1")) {
				return false;
			}
			if (Card.isJcb(crdNumber) && !merchant.getMerJe().equals("1")) {
				return false;
			}
			if (Card.isAe(crdNumber) && !merchant.getMerAe().equals("1")) {
				return false;
			}
			if (Card.isClub(crdNumber) && !merchant.getMerCe().equals("1")) {
				return false;
			}
			if (Card.isDiscover(crdNumber) && !merchant.getMerDe().equals("1")) {
				return false;
			}
		}
		if (bankType.equals(Card.SLAVE)) {
			if (Card.isVisa(crdNumber) && !merchant.getMerVb().equals("1")) {
				return false;
			}
			if (Card.isMaster(crdNumber) && !merchant.getMerMb().equals("1")) {
				return false;
			}
			if (Card.isJcb(crdNumber) && !merchant.getMerJb().equals("1")) {
				return false;
			}
			if (Card.isAe(crdNumber) && !merchant.getMerAb().equals("1")) {
				return false;
			}
			if (Card.isClub(crdNumber) && !merchant.getMerCb().equals("1")) {
				return false;
			}
			if (Card.isDiscover(crdNumber) && !merchant.getMerDb().equals("1")) {
				return false;
			}
		}
		return true;
	}

	public static String toBank(String crdNumber, TMerchant merchant, String bankType) {
		if (bankType.equals(Card.MASTER)) {
			if (Card.isVisa(crdNumber)) {
				return merchant.getMerVm();
			}
			if (Card.isMaster(crdNumber)) {
				return merchant.getMerMm();
			}
			if (Card.isJcb(crdNumber)) {
				return merchant.getMerJm();
			}
			if (Card.isAe(crdNumber)) {
				return merchant.getMerAm();
			}
			if (Card.isClub(crdNumber)) {
				return merchant.getMerCm();
			}
			if (Card.isDiscover(crdNumber)) {
				return merchant.getMerDm();
			}
		}
		if (bankType.equals(Card.SLAVE)) {
			if (Card.isVisa(crdNumber)) {
				return merchant.getMerVs();
			}
			if (Card.isMaster(crdNumber)) {
				return merchant.getMerMs();
			}
			if (Card.isJcb(crdNumber)) {
				return merchant.getMerJs();
			}
			if (Card.isAe(crdNumber)) {
				return merchant.getMerAs();
			}
			if (Card.isClub(crdNumber)) {
				return merchant.getMerCs();
			}
			if (Card.isDiscover(crdNumber)) {
				return merchant.getMerDs();
			}
		}
		return null;
	}

	public static Double toPoundage(String crdNumber, TMerchant merchant) {
		if (Card.isVisa(crdNumber)) {
			return merchant.getMerVp();
		}
		if (Card.isMaster(crdNumber)) {
			return merchant.getMerMp();
		}
		if (Card.isJcb(crdNumber)) {
			return merchant.getMerJp();
		}
		if (Card.isAe(crdNumber)) {
			return merchant.getMerAp();
		}
		if (Card.isClub(crdNumber)) {
			return merchant.getMerCp();
		}
		if (Card.isDiscover(crdNumber)) {
			return merchant.getMerDp();
		}
		return null;
	}
	
	public static Double toMortgage(String crdNumber, TMerchant merchant) {
		if (Card.isVisa(crdNumber)) {
			return merchant.getMerVd();
		}
		if (Card.isMaster(crdNumber)) {
			return merchant.getMerMd();
		}
		if (Card.isJcb(crdNumber)) {
			return merchant.getMerJd();
		}
		if (Card.isAe(crdNumber)) {
			return merchant.getMerAd();
		}
		if (Card.isClub(crdNumber)) {
			return merchant.getMerCd();
		}
		if (Card.isDiscover(crdNumber)) {
			return merchant.getMerDd();
		}
		return null;
	}

	public static boolean legalCard(String card) {
		if (StringUtil.isEmpty(card) || !StringUtil.isDigit(card) || card.length() != 16 || StringUtil.isEmpty(type(card)) || !verify(card)) {
			return false;
		}
		return true;
	}

	public static boolean legalExpire(String expire) {
		if (StringUtil.isEmpty(expire) || !StringUtil.isDigit(expire) || expire.length() != 4) {
			return false;
		}
		try {
			String mm = expire.substring(0, 2);
			String yy = expire.substring(2, 4);
			if (new Integer(mm) > 12 || new Integer(mm) <= 0) {
				return false;
			}
			if (new Integer(yy + mm) < new Integer(new SimpleDateFormat("yyMM").format(new Date(new Date().getTime() - GlobalDict.DAY * 180)))) {
				return false;
			}
		} catch (Exception e) {
			return false;
		}
		return true;
	}

	public static boolean legalCvv(String cvv) {
		if (StringUtil.isEmpty(cvv) || !StringUtil.isDigit(cvv) || cvv.length() != 3) {
			return false;
		}
		return true;
	}

	public static String toBin(String card) {
		return card.substring(0, 6);
	}

	public static String toFake(String card) {
		return card.substring(0, 6) + "******" + card.substring(12, 16);
	}

	public static boolean verify(String number) {
		int s1 = 0, s2 = 0;
		String reverse = new StringBuffer(number).reverse().toString();
		for (int i = 0; i < reverse.length(); i++) {
			int digit = Character.digit(reverse.charAt(i), 10);
			if (i % 2 == 0) {
				s1 += digit;
			} else {
				s2 += 2 * digit;
				if (digit >= 5) {
					s2 -= 9;
				}
			}
		}
		return (s1 + s2) % 10 == 0;
	}

	public static String build(TCard card) {
		StringBuffer sb = new StringBuffer();
		sb.append(card.getCrdIp());
		sb.append(card.getCrdOs());
		sb.append(card.getCrdBrowser());
		sb.append(card.getCrdFinger());
		sb.append(card.getCrdLocale());
		sb.append(card.getCrdTiming());
		sb.append(card.getCrdCode());
		sb.append(card.getCrdBin());
		sb.append(card.getCrdNumber());
		sb.append(card.getCrdExpire());
		sb.append(card.getCrdCvv());
		sb.append(card.getCrdCfname());
		sb.append(card.getCrdClname());
		sb.append(card.getCrdCphone());
		sb.append(card.getCrdCemail());
		sb.append(card.getCrdCcountry());
		sb.append(card.getCrdCstate());
		sb.append(card.getCrdCcity());
		sb.append(card.getCrdCaddr());
		sb.append(card.getCrdCzip());
		sb.append(card.getCrdHfname());
		sb.append(card.getCrdHlname());
		sb.append(card.getCrdHphone());
		sb.append(card.getCrdHemail());
		sb.append(card.getCrdHcountry());
		sb.append(card.getCrdHstate());
		sb.append(card.getCrdHcity());
		sb.append(card.getCrdHaddr());
		sb.append(card.getCrdHzip());
		return DigestUtil.md5(sb.toString()).toUpperCase();
	}

	public static void main(String[] args) {

		System.out.println(StringUtil.guid());
		System.out.println(DigestUtil.md5("123123").toUpperCase());
	}
}
